﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Glue.Data;
using Glue.Data.Mapping;
namespace SqueezeboxBridge
{
    [Table(Name="File")]
    public class StreamCacheFile
    {
        [Key]
        public string Id;
        public string Path;
        public DateTime CacheDate;

        public StreamCacheFile()
        {
        }

        public string FileName
        {
            get
            {
                return System.IO.Path.GetFileName(this.Path);
            }
        }

        public string Extension
        {
            get
            {
                return System.IO.Path.GetExtension(this.Path);
            }
        }

        public string ContentType
        {
            get
            {
                return "audio/mpeg";
            }
        }

        public void Insert()
        {
            Global.DataProvider.Insert(this);
        }

        public void Update()
        {
            Global.DataProvider.Update(this);
        }

        public void Delete()
        {
            Global.DataProvider.Delete(this);
        }

        public static StreamCacheFile FindById(string id)
        {
            return Global.DataProvider.Find<StreamCacheFile>(id);
        }

        public static StreamCacheFile FindByPath(string path)
        {
            return Global.DataProvider.FindByFilter<StreamCacheFile>(Filter.Create("path=@0", path));
        }

        public static StreamCacheFile Create(string path)
        {
            StreamCacheFile file = new StreamCacheFile();
            file.Path = path;
            file.Id = Guid.NewGuid().ToString("N");
            file.CacheDate = DateTime.Now;
            return file;
        }
    }
}
